
import hashlib, base64, random, string

class UserService():

    @staticmethod
    def geneAuthCode(user_info):
        # md5密钥
        m = hashlib.md5()
        str = ("{0}-{1}-{2}-{3}").format(user_info.uid, user_info.login_name,
                                         user_info.login_pwd,
                                         user_info.login_salt)
        # 生成密钥
        m.update(str.encode("utf-8"))
        return m.hexdigest()

    @staticmethod
    def genePwd(pwd, salt):
        m = hashlib.md5()
        str = ("{0}-{1}").format(base64.encodebytes(pwd.encode("utf-8")), salt)
        m.update(str.encode("utf-8"))
        return m.hexdigest()

    @staticmethod
    def geneSalt( length = 16 ):
        keylist = [ random.choice( ( string.ascii_letters + string.digits ) ) for i in range( length ) ]
        return ( "".join( keylist ) )
